<?php
/**
 * Created by PhpStorm.
 * User: ewen
 * Date: 4/27/21
 * Time: 1:39 PM
 */

namespace app\common\model;


class XkUserTypeTask extends Common
{

    /**
     * 根据用户类型获取任务信息
     * @param $userType
     * @return array
     */
    public static function getTaskListByUserType($userType, $userID, $lang = 'zh-cn')
    {
        $where = [
            'a.userType' => $userType
        ];

        $list = collection(self::alias('a')
            ->field('
            a.id as taskID,a.userType,a.ruleID,b.rule_code,b.rule_need_num,b.rule_des,b.coin_num,b.sort,b.groupName
            ')
            ->join('xk_system_rule b', 'a.ruleID = b.id', 'left')
            ->order('b.sort desc')
            ->where($where)
            ->select())->toArray();

        $backArr = [];
        foreach ($list as $k => $v) {
            if ($lang !== 'zh-cn') {
                switch ($v['ruleID']) {
                    case 1:
                        $v['rule_des'] = 'daily sign';
                        break;
                    case 2:
                        $v['rule_des'] = 'invite friends to register and authenticate with real name';
                        break;
                    case 3:
                        $v['rule_des'] = 'options trades up to 10000 U';
                        break;
                    case 4:
                        $v['rule_des'] = 'options trades up to 50000 U';
                        break;
                    case 5:
                        $v['rule_des'] = 'options trades up to 100000 U';
                        break;
                    case 6:
                        $v['rule_des'] = 'contract trades up to 10000 U';
                        break;
                    case 7:
                        $v['rule_des'] = 'contract trades up to 50000 U';
                        break;
                    case 8:
                        $v['rule_des'] = 'contract trades up to 100000 U';
                        break;
                    case 9:
                        $v['rule_des'] = 'join partner';
                        break;
                    case 10:
                        $v['rule_des'] = 'fund trades up to 10000 U';
                        break;
                    case 11:
                        $v['rule_des'] = 'fund trades up to 50000 U';
                        break;
                    case 12:
                        $v['rule_des'] = 'fund trades up to 100000 U';
                        break;
                    case 13:
                        $v['rule_des'] = 'trading contracts or options for 7 consecutive days';
                        break;
                }
            }
            //  判断任务完成度
            switch ($v['groupName']) {
                case 'sign':
                    //  判断是否已签到
                    $hasSigned = XkSignRecord::hasSigned($userID);
                    $v['rule_done_num'] = $hasSigned ? 1 : 0;
                    break;
                case 'invite':
                    //  判断邀请实名人数
                    $count = WebInviter::getSonsCertificationNum($userID);
                    $v['rule_done_num'] = $count;
                    //  数量完成奖励TXUSD
                    WebAssetAccount::awardTxUsd($count, $v['rule_need_num'], $v['coin_num'], 106, $userID);
                    break;
                case 'qiquan':
                    //  期权流水
//                    $num = AssetFlow::getSumData(7, 9002, $userID);
                    $num = OptionAmountItem::getSumData($userID);
                    $v['rule_done_num'] = $num >= $v['rule_need_num'] ? $v['rule_need_num'] : $num;
                    //  数量完成奖励TXUSD
                    WebAssetAccount::awardTxUsd($num, $v['rule_need_num'], $v['coin_num'], 102, $userID);
                    break;
                case 'zhishu':
                    //  TODO EWEN 合约部分表不知道
                    //  指数合约流水
                    $num = AssetFlow::getSumData(7, 8001, $userID);
                    $v['rule_done_num'] = $num >= $v['rule_need_num'] ? $v['rule_need_num'] : $num;
                    //  数量完成奖励TXUSD
                    WebAssetAccount::awardTxUsd($num, $v['rule_need_num'], $v['coin_num'], 103, $userID);
                    break;
                case 'jijin':
                    //  基金流水
//                    $num = AssetFlow::getSumData(7, 9003, $userID);
                    $num = FundAmountItem::getSumData($userID);
                    $v['rule_done_num'] = $num >= $v['rule_need_num'] ? $v['rule_need_num'] : $num;
                    //  数量完成奖励TXUSD
                    WebAssetAccount::awardTxUsd($num, $v['rule_need_num'], $v['coin_num'], 104, $userID);
                    break;
                case 'trade':
                    //  连续交易
                    $num = AssetFlow::getDaysLog($userID);
                    $v['rule_done_num'] = $num >= $v['rule_need_num'] ? $v['rule_need_num'] : $num;
                    //  数量完成奖励TXUSD
                    WebAssetAccount::awardTxUsd($num, $v['rule_need_num'], $v['coin_num'], 105, $userID);
                    break;
                case 'partner':
                    //  加盟合伙人
                    $isPartner = PartnerAccount::isPartner($userID);
                    $v['rule_done_num'] = $isPartner;
                    break;

            }
            $backArr[$v['groupName']][] = $v;
        }

        return $backArr;
    }
}